<template>
    <div class="white-box">
        <title-more title="全局筛选">
            <template #sub>
                <el-popover placement="right-start" title="" :width="300" trigger="hover">
                    <template #reference>
                        <el-icon color="#C1C1C1" :size="18"><QuestionFilled /></el-icon>
                    </template>
                    <template #default>
                        <p class="p-popover">默认筛选当天</p>
                        <p class="p-popover">统计时间：从当天往前推一个月</p>
                    </template>
                </el-popover>
            </template>
        </title-more>
        <div class="task-one" style="margin-bottom: 0">
            <div class="task-one__item">
                <span class="task-one__label">选择时间：</span>
                <el-date-picker v-model="area_time" type="date" value-format="YYYY-MM-DD" :disabled-date="disabledDate" placeholder="请选择时间" />
            </div>
            <div class="task-one__btn">
                <el-button type="primary" :loading="search_btn" :icon="Search" @click="handleSearch">查询</el-button>
            </div>
        </div>
    </div>
    <div class="white-box">
        <title-more title="数据概览">
            <template #sub>
                <el-popover placement="right-start" title="" :width="700" trigger="hover">
                    <template #reference>
                        <el-icon color="#C1C1C1" :size="18"><QuestionFilled /></el-icon>
                    </template>
                    <template #default>
                        <p class="p-popover">注：实时数据仅供即时运营参考，最终以离线数据为准。</p>
                        <p class="p-popover">退款笔数：统计周期内，待处理和已完成退款订单数</p>
                        <p class="p-popover">待处理笔数：统计周期内，申请退款订单数</p>
                        <p class="p-popover">已经完成：统计周期内，已完成的退款订单</p>
                        <p class="p-popover">退款率：统计周期内，售后成功的订单数占支付成功的订单数的比例，等于售后订单数/总订单数</p>
                    </template>
                </el-popover>
            </template>
        </title-more>
        <div class="data-prev flexs">
            <div class="data-prev__item">
                <div class="data-prev__tip">退款笔数</div>
                <p class="data-prev__nums">{{ objs.refund_order || "0" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">待处理笔数</div>
                <p class="data-prev__nums">{{ objs.wait_refund_order || "0" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">已经完成</div>
                <p class="data-prev__nums">{{ objs.finish_refund_order || "0" }}</p>
            </div>
            <div class="data-prev__item">
                <div class="data-prev__tip">退款率</div>
                <p class="data-prev__nums">{{ objs.refund_rate || "0" }}<span v-if="objs.refund_rate">%</span></p>
            </div>
        </div>
    </div>

    <!-- 退款笔数 -->
    <div class="white-box">
        <title-more title="近一月退款笔数"></title-more>
        <div class="echars-one__cont">
            <div id="echart-nums" class="echart-view"></div>
        </div>
    </div>

    <!-- 退款金额 -->
    <div class="white-box">
        <title-more title="近一月退款金额"></title-more>
        <div class="echars-one__cont">
            <div id="echart-money" class="echart-view"></div>
        </div>
    </div>
    <go-back />
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, onBeforeUnmount } from "vue"
import { Search, QuestionFilled } from "@element-plus/icons-vue"
import TitleMore from "@components/title-more.vue"
import GoBack from "@components/GoBack.vue"

// echarts
import EchartsLine from "@common/echarts-line"
import EchartsBar from "@common/echarts-bar"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getDataInfo()
    window.onresize = echartResize
})

onBeforeUnmount(() => {
    echartsDispose()
    window.onresize = null
})

// 概况
const objs = ref({
    refund_order: 0, // 退款笔数
    wait_refund_order: 0, // 待处理笔数
    finish_refund_order: 0, // 已经完成
    refund_rate: 0, // 退款率
})
function getDataInfo() {
    proxy.$axios
        .get("data/refund/tongji", {
            params: {
                day: area_time.value,
            },
        })
        .then((res) => {
            if (res.data.code == 0) {
                let result = res.data.data
                objs.value.refund_order = Number(result.refund_wait_count) + Number(result.refund_finish_count)
                objs.value.wait_refund_order = result.refund_wait_count
                objs.value.finish_refund_order = result.refund_finish_count
                objs.value.refund_rate = result.total_order_num == 0 ? 0 : ((result.refund_total_num / result.total_order_num) * 100).toFixed(2)
                numsEchart()
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {})
}

// 筛选
const area_time = ref("")
const search_btn = ref(false)
const handleSearch = () => {
    search_btn.value = true
    echartsDispose()
    getDataInfo()
}

/**
 * 图表
 * @param {Object} echart_nums 退款笔数
 * @param {Object} echart_money 退款金额
 */
let echart_nums = null
let echart_money = null
function echartResize() {
    echart_nums.resize()
    echart_money.resize()
}
function echartsDispose() {
    echart_nums != null && echart_nums.dispose()
    echart_money != null && echart_money.dispose()
    echart_nums = null
    echart_money = null
}

// 退款笔数-柱状图
function numsEchart() {
    echart_nums = proxy.$echarts.init(document.getElementById("echart-nums"))
    let moneys = [],
        refund_nums = [],
        xData = []
    proxy.$axios
        .get("data/refund/refundRate", {
            params: {
                day: area_time.value,
            },
        })
        .then((res) => {
            search_btn.value = false
            if (res.data.code == 0) {
                let result = res.data.data.refund_items
                result.map((item) => {
                    moneys.push(item.money)
                    refund_nums.push(item.refund_num)
                    xData.push(item.date)
                })
                let series = [
                    {
                        data: refund_nums,
                        type: "bar",
                        name: "退款笔数",
                    },
                ]
                let option = EchartsBar(xData, series)
                echart_nums.setOption(option)

                priceEchart(xData, moneys)
            } else {
                proxy.$message({ message: res.data.msg, type: "error" })
            }
        })
        .catch((err) => {
            search_btn.value = false
        })
}

// 退款金额
function priceEchart(xData, datas) {
    echart_money = proxy.$echarts.init(document.getElementById("echart-money"))
    // 设置配置项
    let series = [
        {
            data: datas,
            type: "line",
            name: "退款金额",
            smooth: true,
            showSymbol: false,
            areaStyle: {
                opacity: 1,
                color: new proxy.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    {
                        offset: 0,
                        color: "rgba(64, 158, 255,0.8)",
                    },
                    {
                        offset: 1,
                        color: "rgba(255, 255, 255, 0.3)",
                    },
                ]),
            },
        },
    ]
    let option = EchartsLine(xData, [], series, "6%")
    echart_money.setOption(option)
}

const disabledDate = (time) => {
    return time.getTime() > Date.now()
}
</script>
<script>
export default {
    name: "RefundAnalytics",
}
</script>

<style lang="less" scoped>
.echart-view {
    width: 100%;
    height: 340px;
}
.p-popover {
    font-size: @font-size-md;
    color: #595961;
    margin: @margin-xs 0px;
}
.data-prev {
    .data-prev__item {
        width: 24%;
        padding: @padding-mdl;
        overflow: hidden;
        border-radius: 10px;
        .data-prev__tip {
            margin-bottom: @margin-md;
            color: @gray-6;
        }
        .data-prev__nums {
            font-size: 26px;
            color: #111;
        }
        &:hover {
            background-color: #f4f8ff;
        }
        .data-prev__ratio {
            margin-top: @margin-base;
            color: @gray-6;
            .ratio-nums {
                color: @red;
                margin-left: -6px;
            }
        }
    }
}
</style>
